package com.aaa.service.impl;

import com.aaa.dao.UserDao;
import com.aaa.entity.User;
import com.aaa.service.UserService;
import com.aaa.util.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @author:LiWei
 * @date:2022年11月17日12:49
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public Result findAll(Integer current, Integer pagesize) {
        Page<User> page = new Page<>(current, pagesize);
        Page<User> page1= userDao.selectPage(page,null);
        System.out.println("总条数:"+page1.getTotal());
        System.out.println("总页码:"+page1.getPages());
        System.out.println("当前页的记录:"+page1.getRecords());
        return new Result(200,"查询成功",page1);
    }

    @Override
    public Result add(User user) {
        int insert = userDao.insert(user);
        return new Result(200,"添加成功",insert);
    }

    @Override
    public Result update(User user) {
        int i = userDao.updateById(user);
        return new Result(200,"修改成功",i);
    }

    @Override
    public Result del(Integer id) {
        int i = userDao.deleteById(id);
        return new Result(200,"删除成功",i);
    }

    @Override
    public Result findByNameAndPassword(String name, String password) {
        User user = userDao.selectByNameAndPassword(name, password);
        if(user!=null){
            //登录成功后保存用户信息再session中
            WebUtil.getSession().setAttribute("user",user);
            return new Result(200,"登录成功");
        }else{
            return new Result(500,"登录失败");
        }
    }
}
